<?xml version="1.0" encoding="UTF-8"?>
<!-- 
 Transforms a refworks xml to custom xhtml with rdf-a
 
 Copyright 2009 The Open University
 j.platts@open.ac.uk
 GNU Public License
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:bibo="http://purl.org/ontology/bibo/" xmlns:address="http://schemas.talis.com/2005/address/schema#">
	<xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" omit-xml-declaration="yes" standalone="no" indent="no" encoding="utf-8"></xsl:output>
	<xsl:param name="count" select="'1'"/>
	
	<xsl:template match="/"> 
	<div id="references_filter{count}" class="references_filter">
		<xsl:apply-templates select=".//reference"/>
	</div>
	</xsl:template>
	
	<xsl:template match="reference">
	<xsl:variable name="id" select="position()"></xsl:variable>
	<div id="rdfa_reference{id}">
	<!--  need to get type (typeof="") and url about="" into this div atts-->
		<xsl:call-template name="title"/>
		<xsl:call-template name="authors"/>
		<xsl:call-template name="journal"/>
		<xsl:call-template name="publisher"/>
		<xsl:call-template name="others"/>
	</div>
	</xsl:template>
	
	<!-- type -->
	<xsl:template name="type">
		<!-- look at value -->
	</xsl:template>
	
	<!-- title -->
	<xsl:template name="title">
		<h2 property="dc:title">
		<!-- make title url? (use u5, lk, ul, ed) -->
		<xsl:choose>
			<xsl:when test="contains(u5, '://')">
				<xsl:call-template name="title_link">
					<xsl:with-param name="uri" select="u5"></xsl:with-param>
					<xsl:with-param name="title" select="t1"></xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="contains(lk, '://')">
				<xsl:call-template name="title_link">
					<xsl:with-param name="uri" select="lk"></xsl:with-param>
					<xsl:with-param name="title" select="t1"></xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="contains(ul, '://')">
				<xsl:call-template name="title_link">
					<xsl:with-param name="uri" select="ul"></xsl:with-param>
					<xsl:with-param name="title" select="t1"></xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="contains(ed, '://')">
				<xsl:call-template name="title_link">
					<xsl:with-param name="uri" select="ed"></xsl:with-param>
					<xsl:with-param name="title" select="t1"></xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="t1"/>
			</xsl:otherwise>
		</xsl:choose>
		</h2>
	</xsl:template>
	<xsl:template name="title_link">
		<xsl:variable name="id" select="position()"></xsl:variable>
		<a href="{$uri}" rel="bibo:uri" about="rdfa_reference{id}"><xsl:value-of select="$title"/></a>
	</xsl:template>
	
	<!-- author(s) -->
	<xsl:template name="authors">
		<xsl:variable name="uid" select="position()"></xsl:variable>
		<p rel="bibo:authorList" resource="#auth_{$uid}" id="auth_{$uid}">
			<xsl:apply-templates select="a1"/>
		</p>
	</xsl:template>
	<xsl:template match="a1">
	<xsl:variable name="uid" select="1 + count(parent::reference/preceding-sibling::reference)"></xsl:variable>
	<xsl:variable name="auid" select="position()"></xsl:variable>
		<span rel="dc:creator" resource="#auths_{$uid}_{$auid}">
			<span resource="#auths_{$uid}_{$auid}" typeof="foaf:Person" id="auths_{$uid}_{$auid}">
				<span property="foaf:name">
					<xsl:value-of select="." />
				</span>;
			</span>
		</span>
	</xsl:template>
	
	<!-- journal/item related info -->
	<xsl:template name="journal">
		<xsl:if test=".//jf or .//jo">
			<xsl:variable name="jid" select="position()"></xsl:variable>
			<p rel="dc:isPartOf" resource="#refjournal{$jid}">
				<span resource="#refjournal{$jid}" typeof="bibo:Journal" id="refjournal{$jid}">
					<xsl:if test=".//jf">
					<span property="dc:title">
						<xsl:apply-templates select="jf" />
					</span>
					</xsl:if>
					<xsl:if test=".//jo">
						<xsl:if test=".//jf">
							<xsl:text>&#32;(</xsl:text>
						</xsl:if>
						<span property="bibo:shortTitle">
							<xsl:apply-templates select="jo" />
						</span>
						<xsl:if test=".//jf">
							<xsl:text>)</xsl:text>
						</xsl:if>
					</xsl:if>
				</span>
			</p>
		</xsl:if>
	</xsl:template>
	
	<!-- publishing info -->
	<xsl:template name="publisher">
		<!-- publisher -->
		<xsl:if test=".//pb or .//pp">
		<xsl:variable name="pid" select="position()"></xsl:variable>
			<div rel="dc:publisher" resource="#pub_{$pid}">
				<div resource="#pub_{$pid}" typeof="foaf:Organization" id="pub_{$pid}">
					<xsl:if test=".//pb">
						<p property="foaf:name"><xsl:apply-templates select="pb" /></p>
					</xsl:if>
					<xsl:if test=".//pp">
						<p property="address:localityName"><xsl:apply-templates select="pp" /></p>
					</xsl:if>
				</div>
			</div>
		</xsl:if>
		<!-- year -->
		<xsl:if test=".//yr">
			<p property="dcterms:date" datatype="xsd:year">
				<xsl:apply-templates select="yr" />
			</p>
		</xsl:if>
	</xsl:template>
	
	
	<!--  all other tags -->
	<xsl:template name="others">
		<!-- hide following properties -->
		<div style="display:none">
		<!-- pages -->
		<xsl:if test=".//sp">
			<p property="bibo:pageStart">
				<xsl:apply-templates select="sp" />
			</p>
		</xsl:if>
		<xsl:if test=".//op">
			<p property="bibo:pages">
				<xsl:apply-templates select="op" />
			</p>
		</xsl:if>
		<!-- Volume -->
		<xsl:if test=".//vo">
			<p property="bibo:volume">
				<xsl:apply-templates select="vo" />
			</p>
		</xsl:if>
		<!-- Issue -->
		<xsl:if test=".//is">
			<p property="bibo:issue">
				<xsl:apply-templates select="is" />
			</p>
		</xsl:if>
		<!-- ISSN/ISBN -->
		<xsl:if test=".//sn">
			<xsl:choose>
				<xsl:when test="contains(.//sn, '-') or string-length(.//sn)=8">
					<p property="bibo:issn">
						<xsl:apply-templates select="sn" />
					</p>
				</xsl:when>
				<xsl:when test="string-length(.//sn)=10">
					<p property="bibo:isbn10">
						<xsl:apply-templates select="sn" />
					</p>
				</xsl:when>
				<xsl:when test="string-length(.//sn)=13">
					<p property="bibo:isbn13">
						<xsl:apply-templates select="sn" />
					</p>
				</xsl:when>
			</xsl:choose>
		</xsl:if>
		<!-- DOI -->
		<xsl:if test=".//do">
			<p property="bibo:doi">
				<xsl:apply-templates select="do" />
			</p>
		</xsl:if>
		<!-- editor -->
		<xsl:if test=".//a2">
			<p property="bibo:editor">
				<xsl:apply-templates select="a2" />
			</p>
		</xsl:if>
		<xsl:if test=".//ed and not(contains(.//ed,'://'))">
			<p property="bibo:editor">
				<xsl:apply-templates select="ed" />
			</p>
		</xsl:if>
		<!-- source -->
		<xsl:if test=".//sr">
			<p property="dc:source">
				<xsl:apply-templates select="sr" />
			</p>
		</xsl:if>
		<!-- Notes -->
		<xsl:if test=".//no">
			<p property="bibo:Note ">
				<xsl:apply-templates select="no" />
			</p>
		</xsl:if>
		<!-- CN -->
		<xsl:if test=".//cn">
			<p property="bibo:lccn ">
				<xsl:apply-templates select="cn" />
			</p>
		</xsl:if>
		</div>
	</xsl:template>

</xsl:stylesheet>